﻿package component.formula.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import component.formula.constant.FormulaConstant;
import component.formula.constant.TreeConstant;

public class TreeOrder implements ITreeOrder {

	// 对一棵树排序必须有下面的字段值
	private String idName = "id";
	private String parentIdName = "parentId";
	private String orderName = "order";
	private String rootId = "0";

	List dataList = null;

	public List orderTree(List treeList) {
		this.dataList = treeList;
		Map rootMap = new HashMap();
		rootMap.put(this.idName, this.getRootId());
		return this.getOrderTreeList(rootMap);
	}

	private List getOrderTreeList(Map parentMap) {
		List treeList = new ArrayList();
		List levelList = new ArrayList();
		List delList = new ArrayList();

		if (dataList == null || dataList.size() == 0) {
			return levelList;
		}

		String parentId = (String) parentMap.get(this.idName);
		Integer parentLevel = (Integer) parentMap
				.get(TreeConstant.LEVEL_NAME); // 父节点的层次

		Iterator it = dataList.iterator();
		while (it.hasNext()) {
			Map objMap = (Map) it.next();
			if (parentId.equals((String) objMap.get(parentIdName))) {
				levelList.add(objMap);
				delList.add(objMap);
			}

		}

		if (delList.size() != 0) {
			dataList.removeAll(delList);
			if (!StringUtil.isNull(this.orderName)) {
				levelList = orderByLevel(levelList);// 对同一层进行排序
			}
			Iterator leveIt = levelList.iterator();
			while (leveIt.hasNext()) {
				Map o = (Map) leveIt.next();
				if (parentLevel == null) {
					o.put(TreeConstant.LEVEL_NAME, new Integer(1));
				} else {
					o.put(TreeConstant.LEVEL_NAME, new Integer(parentLevel
							.intValue() + 1));
				}
				
				treeList.add(o);

				List tList = this.getOrderTreeList(o);// 递归调用自身的方法
				Iterator ittree = tList.iterator();
				while (ittree.hasNext()) {
					treeList.add(ittree.next());
				}
			}

		}
		
		return treeList;
	}

	/**
	 * 对同一层树的数据按照顺序号进行排序
	 * 
	 * @param levelList
	 * @return
	 */
	private List orderByLevel(List levelList) {
		int len = levelList.size();
		if (len == 0)
			return levelList;

		Map map = new HashMap();

		for (int i = 0; i < len; i++) {
			Map objMap = (Map) levelList.get(i);
			Object order = objMap.get(this.orderName);
			List list = (List) map.get(order);
			if (list == null)
				list = new ArrayList();
			list.add(objMap);// 将后面顺序号相同的对象增加到前面的list中
			map.put(order, list);

		}

		// 进行排序
		Object[] keyArr = (Object[]) map.keySet().toArray();
		Arrays.sort(keyArr);
//		Long orderArr[] = new Long[keyArr.length];
//		len = keyArr.length;
//		for (int i = 0; i < len; i++) {
//			orderArr[i] = Long.valueOf(keyArr[i].toString());// 将顺序号放到Long类型的数组中
//		}
//		Arrays.sort(orderArr);// 对顺序号进行排序
		List oList = new ArrayList();
		for (int i = 0; i < len; i++) {
//			List list = (List) map.get(orderArr[i]);
			List list = (List) map.get(keyArr[i]);
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				oList.add(iter.next());
			}
		}
		return oList;
	}

	public List orderTree2(List treeList) {
		this.dataList = treeList;
		Map rootMap = new HashMap();
		rootMap.put(this.idName, this.getRootId());
		return this.getOrderTreeList2(rootMap);
	}
	
	public List getOrderTreeList2(Map parentMap) {
		List treeList = new ArrayList();
		if (dataList == null || dataList.isEmpty()) {
			return treeList;
		}

		Integer parentLevel = (Integer) parentMap.get(TreeConstant.LEVEL_NAME); // 父节点的层次
		String id = (String) parentMap.get(this.idName);
		for (int i = 0; i < dataList.size(); i++) {
			Map rowMap = (Map) dataList.get(i);
			String parentId = (String) rowMap.get(this.parentIdName);
			if (parentId.equals(id)) {
				if (parentLevel == null) {
					rowMap.put(TreeConstant.LEVEL_NAME, new Integer(1));
				} else {
					rowMap.put(TreeConstant.LEVEL_NAME, new Integer(parentLevel
							.intValue() + 1));
				}
				List childList = this.getOrderTreeList2(rowMap);// 递归获取所有子节点
				childList = orderByLevel(childList);// 对同一层进行排序
				rowMap.put("childList", childList);
				treeList.add(rowMap);
			}
		}
		return treeList;
	}
	
	public String getIdName() {
		return idName;
	}

	public void setIdName(String idName) {
		this.idName = idName;
	}

	public String getParentIdName() {
		return parentIdName;
	}

	public void setParentIdName(String parentIdName) {
		this.parentIdName = parentIdName;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}

	public String getRootId() {
		return rootId;
	}

	public void setRootId(String rootId) {
		this.rootId = rootId;
	}
	
	private static List getTreeDataList(){
		//sql1对应的数据
		List dataList1 = new ArrayList();
		
		Map rowMap = null;
		for(int i = 1;i<3;i++){ // 不能从 0 开始 , 0是根节点
			rowMap = new HashMap();
			rowMap.put("id", "" + i);
			rowMap.put("code", "code" + i);
			rowMap.put("name", "name" + i);
			rowMap.put("parentId", "0");
			rowMap.put("order", String.valueOf(i));
			dataList1.add(rowMap);
			
			//第二层数据
			for(int j = 100; j<103;j++){
				rowMap = new HashMap();
				rowMap.put("id", i + "_" + j);
				rowMap.put("code", "code" + i + "_" + j);
				rowMap.put("name", "name" + i + "_"+ j);
				rowMap.put("parentId", "" + i);
				rowMap.put("order", String.valueOf(j));
				dataList1.add(rowMap);
				
				//第三层数据
				for(int k = 1002; k>=1000;k--){
					rowMap = new HashMap();
					rowMap.put("id", i + "_" + j + "_" + k);
					rowMap.put("code", "code" + i + "_" + j + "_" + k);
					rowMap.put("name", "name"  + i + "_" + j + "_" + k);
					rowMap.put("parentId", i + "_" + j);
					rowMap.put("order", String.valueOf(k));
					dataList1.add(rowMap);
				}
			}
			
		}
		
		return dataList1;
	}
	
	private void showData(Map parentMap){
		List childList = (List)parentMap.get(TreeConstant.CHILD_LIST_NAME);
		if(childList!=null){
			for(int i = 0;i<childList.size();i++){
				Map nodeMap = (Map)childList.get(i);
				System.out.println(nodeMap.get("name") + "  " + nodeMap.get(TreeConstant.LEVEL_NAME));
				this.showData(nodeMap);
			}
		}
	}
	
	public static void main(String[] args) {
		List treeList = (List)getTreeDataList();
		
		TreeOrder util = new TreeOrder();
		List retList = (util).orderTree2(treeList);
		
		Map rootMap = new HashMap();
		rootMap.put(TreeConstant.CHILD_LIST_NAME, retList);
		util.showData(rootMap);
	}

}
